#!/bin/env ruby
# encoding: utf-8

def check_performance_page
  a(href: '/en/us/profile').wait_until_present
  h2(text: 'Add projects and tasks').should be_present  
  button(class: "btn btn-default js-add-project-button", 
  	     text: 'Add new project').should be_present
  tableP=table(class: 'table table-striped')
  tableP.wait_until_present
  (tableP[0][0].text == 'Project title').should be_truthy
  (tableP[0][1].text == 'Project description').should be_truthy
  (tableP[0][2].text == 'Status').should be_truthy
end
  
def check_categories_page
  a(href: '/en/us/profile').wait_until_present
  h2(class: "page-header", text: "Engagement factors").wait_until_present(3)
  (browser.text.include? 'Filter by department (company by default):').should be_truthy
  select_list(id: "js-admin-selector-departments",
       class: "form-control min-width-selector" ).wait_until_present(3)
  tableP = table(class: "table table-hover js-admin-table-for-department table-striped")
  tableP.wait_until_present(5)  
  (tableP[0][0].text == 'Engagement factors').should be_truthy
  form(class: "form-inline js-admin-add-form", method: "post").should be_present
  text_field(id: "new-entry-input-js", class: "form-control js-admin-add-input", name: "title",
      placeholder: "Enter new engagement factor for this department").should be_present
  button(class: "btn btn-success js-admin-add-btn", text: "Add new").should be_present
end  

def check_benefit_page
  (browser.text.include? 'Benefits to organization').should be_truthy
  (browser.text.include? 'Filter by department (company by default):').should be_truthy
  select_list(id: "js-admin-selector-departments",  class: "form-control min-width-selector").wait_until_present
  table(class: "table table-hover js-admin-table-for-department table-striped").wait_until_present(5)
  (table(class: "table table-hover js-admin-table-for-department table-striped")[0][0].text ==
    "Benefits to organization").should be_truthy
  text_field(id: "new-entry-input-js", class: "form-control js-admin-add-input", placeholder: "Enter new benefit for this department", 
    name: "title").should be_present
  button(class: "btn btn-success js-admin-add-btn", text: "Add new").wait_until_present

end

def check_signup_page
 (browser.text.include? 'Pulse').should be_truthy  
 a(class: "navbar-brand", href: "/en/us/", text: 'Pulse').wait_until_present(5)
 (browser.text.include? 'Sign up').should be_truthy    
 text_field(class: "form-control", name: "firstName").wait_until_present(5)
 text_field(class: "form-control", name: "lastName").wait_until_present(5)
 text_field(class: "form-control", name: "username").wait_until_present(5)
 text_field(class: "form-control", name: "password").wait_until_present(5)
 text_field(class: "form-control", name: "confirmationPassword").wait_until_present(5)
 text_field(class: "form-control", name: "organizationName").wait_until_present(5)
 button(type: "submit", text: 'OK').wait_until_present(5)
end


def check_profile_page 
  a(href: '/en/us/profile').click
  text_field(id: 'firstName').should be_present
  text_field(id: 'middleName').should be_present
  text_field(id: 'lastName').should be_present  
  text_field(id: 'oldPassword').should be_present
  text_field(id: 'newPassword').should be_present  
  button(name: 'submit').should be_present  
end

def check_collecting_page
  text_field(name: 'subject').should be_present  
  div(class: 'cke_inner cke_reset').should be_present
  input(name: 'frequency').should be_present 
  button(type: 'submit', text: 'Save').should be_present   
 
  (browser.text.include? 'Improvements collecting email template').should be_truthy  
  (browser.text.include? 'Subject of email').should be_truthy  
  (browser.text.include? 'Text of email').should be_truthy  
  (browser.text.include? 'Frequency of email').should be_truthy  
end

def check_standard_report_page  
  a(href: '/en/us/profile').should be_present 
  select_list(id: 'js-report-selector-departments').should be_present
  (inputs(class: "form-control input-sm js-reports-rangepicker").size()==3).should be_truthy

  selectList=select_list(id: 'report-input-benefit')
  selectList.should be_present
  selectList.option(text: 'Not sure').should be_present
  selectList.option(text: 'Better strategic thinking').should be_present
  selectList.option(text: "Increases customer satisfaction").should be_present
  selectList.option(text: "Increases innovation").should be_present
  selectList.option(text: "Increases productivity").should be_present
  selectList.option(text: "Increases profits").should be_present

  (browser.text.include? 'Download reports:').should be_truthy  
  (browser.text.include? 'Improvements').should be_truthy  
  (browser.text.include? 'completed over the period').should be_truthy  
  (browser.text.include? 'filtered by benefit').should be_truthy  
  (browser.text.include? 'over the period').should be_truthy  
  (browser.text.include? 'All users').should be_truthy  
  (browser.text.include? 'Improvement count per month since beginning per organization health')
  .should be_truthy  
  (browser.text.include? 'Request count per month since beginning per engagement')
  .should be_truthy  
end

def check_score_report_page  
  a(href: '/en/us/profile').should be_present   

  (browser.h2.text.include? 'Engagement score report').should be_truthy
  (browser.text.include? 'Filter by department (company by default):').should be_truthy  
  (browser.text.include? 'How do you rate your relationship with your manager?').should be_truthy  
  (browser.text.include? 'How do you rate your work and work environment?').should be_truthy  
  (browser.text.include? 'Are you recognized well enough?').should be_truthy  
  (browser.text.include? 'Do you have trust in leadership?').should be_truthy  
  (browser.text.include? 'Are you motivated to do your work?').should be_truthy  
  (browser.text.include? 'Are customers directly or indirectly happy because of your work?').should be_truthy  
  (browser.text.include? 'Does your work directly or indirectly contribute to higher productivity?').should be_truthy  
  (browser.text.include? 'Does you work directly or indirectly contribute to strategic thinking of your team?').should be_truthy  
  (browser.text.include? 'Does your work directly or indirectly contribute to profitability?').should be_truthy
  (browser.text.include? 'Does your work directly or indirectly contribute to innovation?').should be_truthy
  browser.select_list(id: "js-selector-departments", class: "form-control min-width-selector").should be_present
  browser.input(id: "js-rangepicker", class: "form-control input-sm js-reports-rangepicker", type: "text").should be_present 

  browser.element( tag_name: 'svg').should be_present
end

def check_graph_report_page
  a(href: '/en/us/profile').should be_present   
  (browser.text.include? 'Filter by department (company by default):').should be_truthy  
  browser.select_list(id: "js-selector-departments", class: "form-control min-width-selector").should be_present
  browser.input(id: "js-rangepicker", class: "form-control input-sm js-reports-rangepicker", type: "text").should be_present 
  
  (browser.text.include? 'Graphical reports').should be_truthy
  (browser.text.include? 'Number of improvements in Completed and In Progress State').should be_truthy
  (browser.text.include? 'Number of improvements completed for different engagement factors').should be_truthy
  (browser.text.include? 'Number of improvements added per priority').should be_truthy
  (browser.text.include? 'Number of improvements added per engagement factor').should be_truthy
  (browser.text.include? 'Top 10 managers who has highest number of improvementsadded by their team').should be_truthy
  (browser.text.include? 'Number of improvements added per manager').should be_truthy
  (browser.text.include? 'Average rating of competency per competency').should be_truthy
  (browser.text.include? 'Chart for each competency of average rating per month').should be_truthy
  (browser.text.include? 'Productivity and profitability increase due to improvements done').should be_truthy

  svg0 = browser.div(id: 'js-status-container')
  svg0.element(tag_name: 'svg').should be_present
  
  svg0 = browser.div(id: 'js-completed-category-container')
  svg0.element(tag_name: 'svg').should be_present
  
  svg0 = browser.div(id: 'js-added-priority-container')
  svg0.element(tag_name: 'svg').should be_present
  

  svg0 = browser.div(id: 'js-added-category-container')
  svg0.element(tag_name: 'svg').should be_present
  
  
  svg0 = browser.div(id: 'js-managers-container')
  svg0.element(tag_name: 'svg').should be_present

  svg0 = browser.div(id: 'js-added-department-container')
  svg0.element(tag_name: 'svg').should be_present

  svg0 = browser.div(id: 'js-feedback-container')
  svg0.element(tag_name: 'svg').should be_present

  svg0 = browser.div(id: 'js-linechart-feedback-container')
  svg0.element(tag_name: 'svg').should be_present

  svg0 = browser.div(id: 'js-linechart-benefit-container')
  svg0.element(tag_name: 'svg').should be_present
end

def check_employees
  a(href: '/en/us/profile').wait_until_present   
  (browser.h2.text.include? 'Employees').should be_truthy
  tableT = browser.table(class: 'table table-hover table-employees table-striped js-admin-table-for-department')
  tableT.should be_present
  
  tableT.thead[0].th(text: 'Name').should be_present
  tableT.thead[0].th(text: 'Email').should be_present
  tableT.thead[0].th(text: 'Manager').should be_present
  tableT.thead[0].th(text: 'Department').should be_present
  tableT.thead[0].th(text: 'Role').should be_present


  file_field(id: "upload-employees-input", name: "uploadedFile").wait_until_present
  button(class: "btn btn-info", type: "submit", text: "Upload CSV with employees list").should be_present
  a(href: "/en/us/admin/download-csv-template/", text: 'Download CSV template').should be_present  
  select_list(id: "js-admin-selector-departments", class: "form-control min-width-selector").should be_present
  (browser.text.include?  'Filter by department:').should  be_truthy
end

def check_employee_edit(employeeId,first,middle,last, email, managerId,departmentId,roleName)
  h2(class: "page-header", text: 'Edit employee').should be_present
  label(class: "col-sm-4 control-label", for: "employee-input-firstName",  text: "Employee first name").should be_present
  label(class: "col-sm-4 control-label", for: "employee-input-middleName", text: "Employee middle name").should be_present
  label(class: "col-sm-4 control-label", for: "employee-input-lastName",   text: "Employee last name").should be_present
  label(class: "col-sm-4 control-label", for: "employee-input-description",text: "Employee email").should be_present
  label(class: "col-sm-4 control-label", for: "employee-input-manager",    text: "Manager").should be_present
  label(class: "col-sm-4 control-label", for: "employee-input-department", text: "Department").should be_present
  label(class: "col-sm-4 control-label", for: "employee-input-role",       text: "Role").should be_present

  text_field(id: "employee-input-firstName", class: "form-control",  name: "firstName", value: "#{first}", placeholder: "Enter first name").should be_present
  text_field(id: "employee-input-middleName", class: "form-control", name: "middleName", placeholder: "Enter middle name").should be_present
  text_field(id: "employee-input-lastName",  class: "form-control",  name: "lastName", value: "#{last}", placeholder: "Enter last name").should be_present
  text_field(id: "employee-input-email",     class: "form-control",  name: "email",    value: "#{email}", placeholder: "Enter email", type: "email").should be_present


  
  select_list(id: "employee-input-manager", class: "form-control",    value: "#{managerId}", name: "managerId").should be_present 
  select_list(id: "employee-input-department", class: "form-control", value: "#{departmentId}", name: "departmentId").should be_present
  (select_list(id: "employee-input-role", class: "form-control", name: "roleId").selected_options[0].text == roleName).should be_truthy
  

  button(class: "btn btn-success", type: "submit", text: "Submit").should be_present

  #managers
  managerList = find_managers(departmentId)  
  opts = Array.new
  select_list(id: "employee-input-manager", class: "form-control").options.each do |option|
    opts << option.text
  end 

  #roles
  opts.clear
  select_list(id: "employee-input-role", class: "form-control").options.each do |option|
    opts << option.text
  end
  opts.eql?(["Requester", "Actioner", "Sponsor", "Admin"]).should be_truthy
  
  #departments
  url = browser.url
  goto 'http://pulse.7bits.it/en/us/admin/employees-list'
  deptOptions = Array.new
  select_list(id: "js-admin-selector-departments", class: "form-control min-width-selector").options.each do |option|
    deptOptions << option.text
  end
  goto url
  opts.clear
  select_list(id: "employee-input-department", class: "form-control").options.each do |option|
    opts << option.text
  end
  opts.eql?(deptOptions).should be_truthy
end